<?php
/**
 *ucenter 同步接口
 *此控制器用于与discuz论坛同步登陆登出
 *使用此接口需要在ucenter中进行配置
 *同时需要开启url rewrite才能正确配置
 *如果不能开启url rewrite 需要将[另外提供]的api文件夹拷到根目录
 *@author oisc
 */
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');

class UcController extends Controller{
	public function index(){
		global $_GET;
		$code = @$_GET['code'];
		//反序列化get post
		parse_str(uc_authcode($code, 'DECODE', UC_KEY), $get);
		$get = uc_stripslashes($get);
		$post = uc_unserialize(file_get_contents('php://input'));
		if(empty($get)){
			exit(API_RETURN_FORBIDDEN);
		}
		//执行action
		$action = $get['action'];
		if(in_array($action, array('test', 'synlogin', 'synlogout', 'updateapps'))){
			//将结果输出
			exit($this->$action($get, $post));
		}else{
			exit(API_RETURN_FAILED);
		}
	}
	//测试 接口
	private function test($get, $post){
		return API_RETURN_SUCCEED;
	}
	//同步登陆
	private function synlogin($get, $post){
		Authhelper::login($get['uid'], $get['username']);
		return API_RETURN_SUCCEED;
	}
	//同步登出
	private function synlogout($get, $post){
		Authhelper::logout();
		return API_RETURN_SUCCEED;
	}
	//client 列表升级
	private function updateapps($get, $post){
		$UC_API = '';
		if($post['UC_API']) {
			$UC_API = str_replace(array('\'', '"', '\\', "\0", "\n", "\r"), '', $post['UC_API']);
			unset($post['UC_API']);
		}
		$cachefile = UC_ROOT.'data/cache/apps.php';
		$fp = fopen($cachefile, 'w');
		$s = "<?php\r\n";
		$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
		fwrite($fp, $s);
		fclose($fp);
		return API_RETURN_SUCCEED;
	}
}